# tempfile --- 生成临时文件和目录
import tempfile

temp = tempfile.TemporaryFile()
print(temp)  # <tempfile._TemporaryFileWrapper object at 0x00000163EF32BB70>
print(temp.name)  # C:\Users\m1761\AppData\Local\Temp\tmphmmdx86i
temp.write(b'hello world')
temp.seek(0)
print(temp.read())  # b'hello world'
# 关闭文件
temp.close()

with tempfile.TemporaryFile() as fp:
    fp.write(b'Hello world!')
    fp.seek(0)
    print(fp.read())  # b'Hello world!'

# 临时文件夹
with tempfile.TemporaryDirectory() as tmpdirname:
    print('created temporary directory', tmpdirname)

